package com.mengye.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mengye.bean.CourseInfoBean;
import com.mengye.bean.TeacherInfoBean;
import com.mengye.bean.UserInfoBean;
import com.mengye.db.DBProvider;
import com.mengye.utils.EasyFactory;
import com.mengye.utils.MyUtils;

public class CourseInfoModel {
	public List<CourseInfoBean> queryLoginInfo(CourseInfoBean courseInfoB) {
		// 声明查询语句，未确定项用？通配符
		String sql = "select * from course where T_id = ? and c_Name =?";
		// 进行查询

		List<CourseInfoBean> lst = EasyFactory.getNewInstance(DBProvider.class).query(sql,
				new BeanListHandler<CourseInfoBean>(CourseInfoBean.class), courseInfoB.getT_id(),
				courseInfoB.getC_Name());
		
		return lst;
	}

	/**
	 * @param courseInfoB1 原数据
	 * @param courseInfoB2	修改的数据
	 * @return
	 */
	public int updateCourseInfo(CourseInfoBean courseInfoB1,CourseInfoBean courseInfoB2) {
		int i = 0;
		
		StringBuilder sbSql = new StringBuilder(" update course set T_id = ? ");
		// 使用list存储sql的？占位符的条件参数
		List<Object> lstParams1 = new ArrayList<>();
		List<Object> lstParams2 = new ArrayList<>();
		lstParams1.add(courseInfoB2.getT_id());
		
		//修改课程名
		if (courseInfoB2.getC_Name() != null && !courseInfoB2.getC_Name().equals("")) {
			// 添加第一个参数，set语句的参数
			sbSql.append(", c_Name = ? ");
			lstParams1.add(courseInfoB2.getC_Name());
		}
		
		//修改课程内容
		if (courseInfoB2.getC_Con() != null && !courseInfoB2.getC_Con().equals("")) {
			// 添加第一个参数，set语句的参数
			sbSql.append(", c_Con = ? ");
			lstParams1.add(courseInfoB2.getC_Con());
		}
		
		//修改课程地点
		if (courseInfoB2.getC_Place() != null && !courseInfoB2.getC_Place().equals("")) {
			// 添加第一个参数，set语句的参数
			sbSql.append(", c_Place = ? ");
			lstParams1.add(courseInfoB2.getC_Place());
		}
		
		//修改课程日期
		if (courseInfoB2.getC_Date() != null && !courseInfoB2.getC_Date().equals("")) {
			// 添加第一个参数，set语句的参数
			sbSql.append(", c_Date = ? ");
			lstParams1.add(courseInfoB2.getC_Date());
		}
		
		//修改课程时间
		if (courseInfoB2.getC_Time() != null && !courseInfoB2.getC_Time().equals("")) {
			// 添加第一个参数，set语句的参数
			sbSql.append(", c_Time = ? ");
			lstParams1.add(courseInfoB2.getC_Time());
		}
		sbSql.append(" where c_Name = ? and c_Con = ? and c_Place = ? and c_Date = ? and c_Time =? and T_id = ?");
		lstParams2.add(courseInfoB1.getC_Name());
		lstParams2.add(courseInfoB1.getC_Con());
		lstParams2.add(courseInfoB1.getC_Place());
		lstParams2.add(courseInfoB1.getC_Date());
		lstParams2.add(courseInfoB1.getC_Time());
		lstParams2.add(courseInfoB1.getT_id());
		i = EasyFactory.getNewInstance(DBProvider.class).update(sbSql.toString(), lstParams1.toArray(),lstParams2.toArray());
		return i;
	}
	
	/**
	 * 保存操作
	 * @param courseInfoB
	 * @return
	 */
	public int saveCourseInfo(CourseInfoBean courseInfoB) {
		int i = 0;
		String sql = "insert into course(c_name,c_con,c_place,c_time,c_date,t_id) values(?,?,?,?,?,?)";
		i = EasyFactory.getNewInstance(DBProvider.class).update(sql,
				new Object[] { courseInfoB.getC_Name(), courseInfoB.getC_Con(), courseInfoB.getC_Place(),
						courseInfoB.getC_Time(), MyUtils.convertDate2String(courseInfoB.getC_Date(), "yyyy-mm-dd"),
						courseInfoB.getT_id() });
		return i;
	}

	public List<CourseInfoBean> queryCourseInfo(CourseInfoBean courseInfoB) {
		// 拼写登录查询的sql语句
		StringBuilder sbSql = new StringBuilder(" select * from course where t_id = ? ");
		// 使用list存储sql的？占位符的条件参数

		List<Object> lstParams = new ArrayList<>();
		lstParams.add(courseInfoB.getT_id());
		if (courseInfoB.getC_Name() != null) {
			// 动态拼写Sql条件
			String strCourseName = courseInfoB.getC_Name();
			if (strCourseName != null && !strCourseName.equals("")) {
				// 如果用户名不为空，则条件用户名查询过滤条件
				sbSql.append(" and c_Name like ? ");
				// 同时对应添加查询条件集合
				lstParams.add("%" + strCourseName + "%");
			}
		}
		sbSql.append(" order by c_Date ");
		List<CourseInfoBean> lst = EasyFactory.getNewInstance(DBProvider.class).query(sbSql.toString(),
				new BeanListHandler<CourseInfoBean>(CourseInfoBean.class), lstParams.toArray());
		System.out.println(lst.size());
		// 返回查询结果集
		return lst;
	}
	
	public List<TeacherInfoBean> quetyPath(TeacherInfoBean teacherInfoB){
		
		String sql = "select * from teacher where t_id = ?";
		List<TeacherInfoBean> lst = EasyFactory.getNewInstance(DBProvider.class).query(sql, new BeanListHandler<TeacherInfoBean>(TeacherInfoBean.class), teacherInfoB.getT_id());
		return lst;
	}
	
	/**
	 * 保存操作
	 * @param courseInfoB
	 * @return
	 */
	public int saveCourseInfo1(List<CourseInfoBean> lst) {
		int i = 0;
		String sql = "insert into course(c_name,c_con,c_place,c_time,c_date,t_id) values(?,?,?,?,?,?)";
		
		for (CourseInfoBean cIB : lst) {
			
			i = EasyFactory.getNewInstance(DBProvider.class).update(sql,cIB.getC_Name(),cIB.getC_Con(),
					cIB.getC_Place(),cIB.getC_Time(),cIB.getC_Date(),cIB.getT_id());
		}
		return i;
	}

	public List<CourseInfoBean> queryOtherCourseInfo(TeacherInfoBean teacherInfoB) {
		
		String sql = "select * from course where t_id = (select t_id from teacher where t_name = ?)";
		List<CourseInfoBean> lst = EasyFactory.getNewInstance(DBProvider.class).query(sql,
				new BeanListHandler<CourseInfoBean>(CourseInfoBean.class), teacherInfoB.getT_Name());
		return lst;
	}
}
